package com.shop.cereshop.app.tencent.im.model.transfer;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.shop.cereshop.commons.tencent.im.constant.CustomMsgModelType;
import com.shop.cereshop.commons.tencent.im.model.msg.data.CustomMsgData;
import com.shop.cereshop.commons.tencent.im.model.msg.data.CustomMsgDataModel;
import com.shop.cereshop.commons.tencent.im.model.msg.dto.MsgRequestDTO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @description: 转账订单表
 * @author zdd
 * @date 2025/9/3 14:30
 * @version 1.0
 */
@ApiModel(description="转账订单表")
@Entity
@TableName("cere_transfer_order")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TransferOrder implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value="转账订单ID（业务全局唯一，用于幂等）")
    @Id
    @TableId(value = "transfer_id", type = IdType.INPUT)
    private String transferId;

    @ApiModelProperty("单号")
    private String orderId;

    @ApiModelProperty(value="付款方用户ID")
    private String senderId;

    @ApiModelProperty(value="付款方用户名")
    private String senderName;

    @ApiModelProperty("群组ID (如果是单聊则为空)")
    private String groupId;

    @ApiModelProperty(value="收款方用户ID")
    private String receiverId;

    @ApiModelProperty(value="收款方用户名")
    private String receiverName;

    @ApiModelProperty(value="转账金额（单位：元）")
    private BigDecimal amount;

    @ApiModelProperty(value="手续费（单位：元，微信转账通常为0）")
    private BigDecimal fee;

    @ApiModelProperty(value="实际到账金额（单位：元）")
    private BigDecimal actualAmount;

    @ApiModelProperty(value="支付方式 (1:微信 2:支付宝 3:钱包余额 4:银行卡 5:其他)")
    private Integer payMethod;

    @ApiModelProperty(value="用户填写的转账备注")
    private String remark;

    @ApiModelProperty(value="订单状态: 1-待支付, 2-支付中, 3-待领取, 4-已领取, 5-已过期, 6-已退款, 7-支付失败")
    private Integer status;

    @ApiModelProperty(value="关联的IM-单聊消息MsgId")
    private String msgId;

    @ApiModelProperty(value="关联的IM-单聊消息MsgKey")
    private String msgKey;

    @ApiModelProperty(value="关联的IM-群聊消息MsgSeq")
    private Long msgSeq;

    @ApiModelProperty(value="订单创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty(value="发送时间")
    private LocalDateTime sendTime;

    @ApiModelProperty(value="支付完成时间")
    private LocalDateTime payTime;

    @ApiModelProperty(value="过期时间（仅在待领取状态时有值）")
    private LocalDateTime expireTime;

    @ApiModelProperty(value="领取时间（领取后填充）")
    private LocalDateTime receiveTime;

    @ApiModelProperty(value="退款时间（过期或主动退款后填充）")
    private LocalDateTime refundTime;

    @ApiModelProperty(value="退款类型 (1:过期自动退款  2:用户主动退款)")
    private Integer refundType;

    @ApiModelProperty(value="最终完成时间（领取/过期/退款）")
    private LocalDateTime completedTime;

    public TransferOrderVO covertToVO() {
        TransferOrderVO vo = new TransferOrderVO();
        vo.setTransferId(this.getTransferId());
        vo.setSenderId(this.getSenderId());
        vo.setSenderName(this.getSenderName());
        vo.setGroupId(this.getGroupId());
        vo.setReceiverId(this.getReceiverId());
        vo.setReceiverName(this.getReceiverName());
        vo.setAmount(this.getAmount());
        vo.setFee(this.getFee());
        vo.setActualAmount(this.getActualAmount());
        vo.setRemark(this.getRemark());
        vo.setStatus(this.getStatus());
//        vo.setMsgId(this.getMsgId());
//        vo.setMsgKey(this.getMsgKey());
//        vo.setMsgSeq(this.getMsgSeq());
        vo.setCreateTime(this.getCreateTime());
        vo.setSendTime(this.getSendTime());
        vo.setExpireTime(this.getExpireTime());
        vo.setPayTime(this.getPayTime());
        vo.setReceiveTime(this.getReceiveTime());
        vo.setRefundTime(this.getRefundTime());
        vo.setRefundType(this.getRefundType());
        vo.setCompletedTime(this.getCompletedTime());
        return vo;
    }

    /**
     * 构建转账订单自定义消息数据
     * @return 自定义消息数据
     */
    public CustomMsgData covertToCustomMsgData() {
        CustomMsgData msgData = new CustomMsgData();
        msgData.setData(this.covertToMsgData());
        msgData.setDesc(this.getRemark());
        msgData.setExt("");
        msgData.setSound("");
        return msgData;
    }

    /**
     * 构建转账订单自定义消息传输数据
     * @return 自定义消息传输数据
     */
    public MsgRequestDTO covertToMsgRequestDTO() {
        return MsgRequestDTO.builder()
                .fromAccount(this.getSenderId())
                .toAccount(this.getReceiverId())
                .groupId(this.getGroupId())
                .title("您有一笔转账订单")
                .desc(this.getRemark())
                .build();
    }

    /**
     * 构建转账订单自定义消息数据
     * @return 自定义消息数据
     */
    public String covertToMsgData() {
        CustomMsgDataModel<Object> dataModel = new CustomMsgDataModel<>();
        dataModel.setVersion("1.0");
        dataModel.setType(CustomMsgModelType.TRANSFER);
        dataModel.setData(covertToVO());
        dataModel.setTimestamp(System.currentTimeMillis());
        return JSON.toJSONString(dataModel);
    }

}